//
//  updatenameController.m
//  pacc
//
//  Created by 测试 on 2018/6/28.
//  Copyright © 2018年 测试. All rights reserved.
//


#import "updatenameController.h"

@interface updatenameController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *nameTx;
@property(nonatomic,strong)UIBarButtonItem * saveButton;
@end

@implementation updatenameController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatNavRightBtn];
    _nameTx.text = _nickName;
}

-(void)creatNavRightBtn{
    UIBarButtonItem * saveButton= [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save)];
    _saveButton = saveButton;
//    saveButton.enabled = NO;
    [saveButton setTintColor:[UIColor whiteColor]];
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:saveButton,nil]];
}
-(void)save{
    NSMutableDictionary *dic =[NSMutableDictionary dictionary];
    [dic setValue:_nameTx.text forKey:@"userName"];
    [dic setValue:_userDesc forKey:@"userDesc"];
    [dic setValue:my_AppDelegate.token forKey:@"token"];
    [SVProgressHUD showWithStatus:@"正在保存"];
    __weak typeof(self) weakself = self;
    
    if ([_nameTx.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"不能为空"];
        return;
    }
    if (_nameTx.text.length>10) {
        [SVProgressHUD showErrorWithStatus:@"不能超过十个字"];
        return;
    }
    [NetWorking startPOSTRequestWithUrl:@"userupdate" parameters:dic logMes:@"更改用户信息" show:NO success:^(NSDictionary *data) {
        if (weakself.returnnameBlock) {
            //将自己的值传出去，完成传值
            weakself.returnnameBlock(self->_nameTx.text);
        }
        [SVProgressHUD dismiss];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        
    }];
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    if ([_nameTx.text isEqualToString:_nickName]) {
        _saveButton.enabled = NO;
    }else{
        _saveButton.enabled = YES;
    }
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//    NSLog(@"---%@---",_nickName)
//    if ([_nameTx.text isEqualToString:_nickName]) {
//        NSLog(@"---%@",_nameTx.text)
//        _saveButton.enabled = NO;
//    }else{
//        NSLog(@"===%@",_nameTx.text)
//
//        _saveButton.enabled = YES;
//    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
